using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using nDeveloper.BO.Base;
using nDeveloper.BO.Base.Domain;
using WeifenLuo.WinFormsUI;
using WeifenLuo.WinFormsUI.Docking;

namespace nDeveloper.UI.Windows.IDE
{
    public partial class frmStoredProcedure : DockContent
    {
        private Constants.vsDataBase TipoServidor = Constants.vsDataBase.Nothing;

        private void carregaConfiguracaoTipos()
        {
            Constants.vsDataBase TipoBD = DataBaseConfig.GetServerType();

            if (TipoBD == Constants.vsDataBase.SQLServer2000)
            {
                cmbTipoServidor.SelectedIndex = 0;
            }
            else if (TipoBD == Constants.vsDataBase.Oracle)
            {
                cmbTipoServidor.SelectedIndex = 1;
            }
            else if (TipoBD == Constants.vsDataBase.PostgreSQL)
            {
                cmbTipoServidor.SelectedIndex = 2;
            }
            else if (TipoBD == Constants.vsDataBase.MySQL)
            {
                cmbTipoServidor.SelectedIndex = 3;
            }
            else if (TipoBD == Constants.vsDataBase.SQLServer2005)
            {
                cmbTipoServidor.SelectedIndex = 4;
            }
        }
        
        public frmStoredProcedure()
        {
            InitializeComponent();

            carregaConfiguracaoTipos();
        }

        private void carregaStoredProcedures()
        {
            if (this.cmbTipoServidor.Text == "SQL Server 2000")
            {
                ListaProcedures(Constants.vsDataBase.SQLServer2000);
                TipoServidor = Constants.vsDataBase.SQLServer2000;
            }
            else if (this.cmbTipoServidor.Text == "SQL Server 2005")
            {
                ListaProcedures(Constants.vsDataBase.SQLServer2005);
                TipoServidor = Constants.vsDataBase.SQLServer2005;
            }
            else if (this.cmbTipoServidor.Text == "Oracle")
            {
                ListaProcedures(Constants.vsDataBase.Oracle);
                TipoServidor = Constants.vsDataBase.Oracle;
            }
            else if (this.cmbTipoServidor.Text == "PostgreSQL")
            {
                ListaProcedures(Constants.vsDataBase.PostgreSQL);
                TipoServidor = Constants.vsDataBase.PostgreSQL;
            }
            else if (this.cmbTipoServidor.Text == "MySQL")
            {
                ListaProcedures(Constants.vsDataBase.MySQL);
                TipoServidor = Constants.vsDataBase.MySQL;
            }
        }

        private void cmbTipoServidor_SelectedIndexChanged(object sender, EventArgs e)
        {
            carregaStoredProcedures();
        }

        private void ListaProcedures(Constants.vsDataBase TipoServidor)
        {
            this.lvProcedures.Items.Clear();

            SystemProcedure sp = new SystemProcedure();
            sp.DataBaseServerType = TipoServidor;

            ArrayList lp = sp.GetProcedureListByDataBaseServerType();
            foreach (SystemProcedure p in lp)
            {
                this.lvProcedures.Items.Add(p.Name, p.Name, 0);
                this.lvProcedures.Items[p.Name].SubItems.Add(p.Description);
            }

            if (lp.Count > 0)
            {
                cmdAlterar.Enabled = true;
                cmdExcluir.Enabled = true;
            }
            else
            {
                cmdAlterar.Enabled = false;
                cmdExcluir.Enabled = false;
            }
        }

        private void cmdNova_Click(object sender, EventArgs e)
        {
            frmEditStoredProcedure frm = new frmEditStoredProcedure(TipoServidor);
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
            {
                ListaProcedures(TipoServidor);
            }
        }

        private void cmdAlterar_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lvProcedures.CheckedItems)
            {
                frmEditStoredProcedure frm = new frmEditStoredProcedure(TipoServidor, item.Name);
                frm.ShowDialog();

                if (frm.DialogResult == DialogResult.OK)
                {
                    ListaProcedures(TipoServidor);
                }

                break;
            }
        }

        private void cmdExcluir_Click(object sender, EventArgs e)
        {
            SystemProcedure sp = new SystemProcedure();
            foreach (ListViewItem item in this.lvProcedures.CheckedItems)
            {
                sp.Name = item.Name;
                sp.DataBaseServerType = TipoServidor;
                sp.Delete();
            }

            ListaProcedures(TipoServidor);
        }

    }
}